using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using DG.Tweening;
using Fleck;
using Newtonsoft.Json;
using RenderHeads.Media.AVProVideo;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class Player : MonoBehaviour
{
    
    //=================================
    public RawImage yixiangImage;

    public Text showText;

    private Tween tween;
  

    private int unity当前仪式人 = -1;
    
    private int unity当前仪式环节 = -1;

    private static object dicLock = new object();
    
    public RenderTexture rt;

    public ResolveToRenderTexture rtrt;

    //背景视频播放器
    public MediaPlayer mediaPlayer;

    //小素材播放器
    public MediaPlayer sCMediaPlayer;

    public DisplayUGUI displayUGUI;
    
    // private List<Bg> list=new List<Bg>();


    
    // private int order=-1;

   
    private void Awake()
    {
       
    }

    
        
    


    // Start is called before the first frame update
    void Start()
    {
        // list.Add(bg1);
        rt.width = 13560;
        rt.height = 1200;
        mediaPlayer.Events.AddListener(OnVideoEvent);
        rtrt.ExternalTexture = null;
        mediaPlayer.OpenMedia(MediaPathType.AbsolutePathOrURL,"E:\\环节\\01烛光祈福\\s01 烛光祈福（待场首播\uff0b循环）.mp4",false);
        mediaPlayer.Control.SetLooping(true);
    }

    public GameObject sp;

    public GameObject tp;

    public GameObject wz;

    public GameObject zong;
    // Update is called once per frame
    void Update()
    {
        
        // if (Input.GetKey(KeyCode.DownArrow))
        // {
        //     if (order<list.Count-1)
        //     {
        //         order++;
        //         rtrt.ExternalTexture = null;
        //         mediaPlayer.OpenMedia(MediaPathType.AbsolutePathOrURL,list[order].noLoop,false);
        //         mediaPlayer.Control.SetLooping(false);
        //     }
        // }
        //
        // if (Input.GetKey(KeyCode.UpArrow))
        // {
        //     if (order>=1)
        //     {
        //         order--;
        //         rtrt.ExternalTexture = null;
        //         mediaPlayer.OpenMedia(MediaPathType.AbsolutePathOrURL,list[order].noLoop,false);
        //         mediaPlayer.Control.SetLooping(false);
        //     }
        //    
        // }
        lock (FuWuQi.fuwuqiLock)
        {
            if (判断切换条件)
            {
            
                zong.SetActive(false);
                mediaPlayer.CloseMedia();
                rtrt.ExternalTexture = null;
                mediaPlayer.OpenMedia(MediaPathType.AbsolutePathOrURL,Application.dataPath+"/hanmengqi.mp4",false);
                mediaPlayer.Control.SetLooping(true);
            }
            else
            {
                if ( 判断切换条件)
                {
                    tp.SetActive(false);
                    sp.SetActive(false);
                    wz.SetActive(false);
                    if (tween!=null)
                    {
                        tween.Kill();
                    }
                    zong.SetActive(true);
                    mediaPlayer.CloseMedia();
                    
                    
                    if (切换条件)
                    {
                        zong.SetActive(false);
                        rtrt.ExternalTexture = null;
                        mediaPlayer.OpenMedia(MediaPathType.AbsolutePathOrURL,Application.dataPath+"/hanmengqi.mp4",false);
                        mediaPlayer.Control.SetLooping(true);
                    }
                    else
                    {
                        rtrt.ExternalTexture = null;
                        mediaPlayer.OpenMedia(MediaPathType.AbsolutePathOrURL,FuWuQi.背景不循环视频,false);
                        if (交互类型=="图片")
                        {
                            tp.SetActive(true);
                            Texture2D texture = new Texture2D(1, 1);
                            if (yixiangImage.texture!=null)
                            {
                                Destroy(yixiangImage.texture);
                            }
                            // 加载图片数据
                            try
                            {
                                byte[] imageData = File.ReadAllBytes(图片信息);
                                // 将图片数据加载到Texture2D对象中
                                texture.LoadImage(imageData);
                            }
                            catch (Exception e)
                            {
                                
                            }
                            yixiangImage.texture = texture;
                        }
                        else if (交互类型 == "视频")
                        {
                            sp.SetActive(true);
                            try
                            {
                                sCMediaPlayer.OpenMedia(MediaPathType.AbsolutePathOrURL,
                                    视频信息, true);
                            }
                            catch (Exception e)
                            {
                                
                            }
                        }
                        else if (交互类型 == "文本")
                        {
                            wz.SetActive(true);
                            try
                            {
                                showText.text = "";
                                tween = showText.DOText(文本信息,文本信息.Length/4f);
                            }
                            catch (Exception e)
                            {
                                
                            }
                            
                        }
                        else
                        {
                           zong.SetActive(false);
                        }
                        mediaPlayer.Control.SetLooping(false);
                    }
                }
            }
            
           
          //发送逻辑
    }
    
    void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode er)
    {
        switch (et)
        {
            //第一帧准备
            case MediaPlayerEvent.EventType.FirstFrameReady:
                print("FirstReady");
                rtrt.ExternalTexture = rt;
                mediaPlayer.Control.Play();
                break;
            //不循环播放完毕
            case MediaPlayerEvent.EventType.FinishedPlaying:
                print("FinishedPlaying");
                rtrt.ExternalTexture = null;
                mediaPlayer.OpenMedia(MediaPathType.AbsolutePathOrURL,背景循环视频,false);
                mediaPlayer.Control.SetLooping(true);
                break;
        }
    }
    
}
